
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
  <head>
    <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>django.contrib.auth.password_validation &#8212; Django 2.2.12.dev20200304094918 documentation</title>
    <link rel="stylesheet" href="../../../../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../../../../_static/pygments.css" type="text/css" />
    <script type="text/javascript" id="documentation_options" data-url_root="../../../../" src="../../../../_static/documentation_options.js"></script>
    <script type="text/javascript" src="../../../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../../../_static/doctools.js"></script>
    <script type="text/javascript" src="../../../../_static/language_data.js"></script>
    <link rel="index" title="Index" href="../../../../genindex.html" />
    <link rel="search" title="Search" href="../../../../search.html" />



 
<script type="text/javascript" src="../../../../templatebuiltins.js"></script>
<script type="text/javascript">
(function($) {
    if (!django_template_builtins) {
       // templatebuiltins.js missing, do nothing.
       return;
    }
    $(document).ready(function() {
        // Hyperlink Django template tags and filters
        var base = "../../../../ref/templates/builtins.html";
        if (base == "#") {
            // Special case for builtins.html itself
            base = "";
        }
        // Tags are keywords, class '.k'
        $("div.highlight\\-html\\+django span.k").each(function(i, elem) {
             var tagname = $(elem).text();
             if ($.inArray(tagname, django_template_builtins.ttags) != -1) {
                 var fragment = tagname.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + tagname + "</a>");
             }
        });
        // Filters are functions, class '.nf'
        $("div.highlight\\-html\\+django span.nf").each(function(i, elem) {
             var filtername = $(elem).text();
             if ($.inArray(filtername, django_template_builtins.tfilters) != -1) {
                 var fragment = filtername.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + filtername + "</a>");
             }
        });
    });
})(jQuery);</script>

  </head><body>

    <div class="document">
  <div id="custom-doc" class="yui-t6">
    <div id="hd">
      <h1><a href="../../../../index.html">Django 2.2.12.dev20200304094918 documentation</a></h1>
      <div id="global-nav">
        <a title="Home page" href="../../../../index.html">Home</a>  |
        <a title="Table of contents" href="../../../../contents.html">Table of contents</a>  |
        <a title="Global index" href="../../../../genindex.html">Index</a>  |
        <a title="Module index" href="../../../../py-modindex.html">Modules</a>
      </div>
      <div class="nav">
    <a href="../../../index.html" title="Module code" accesskey="U">up</a></div>
    </div>

    <div id="bd">
      <div id="yui-main">
        <div class="yui-b">
          <div class="yui-g" id="_modules-django-contrib-auth-password_validation">
            
  <h1>Source code for django.contrib.auth.password_validation</h1><div class="highlight"><pre>
<span></span><span class="kn">import</span> <span class="nn">functools</span>
<span class="kn">import</span> <span class="nn">gzip</span>
<span class="kn">import</span> <span class="nn">re</span>
<span class="kn">from</span> <span class="nn">difflib</span> <span class="k">import</span> <span class="n">SequenceMatcher</span>
<span class="kn">from</span> <span class="nn">pathlib</span> <span class="k">import</span> <span class="n">Path</span>

<span class="kn">from</span> <span class="nn">django.conf</span> <span class="k">import</span> <span class="n">settings</span>
<span class="kn">from</span> <span class="nn">django.core.exceptions</span> <span class="k">import</span> <span class="p">(</span>
    <span class="n">FieldDoesNotExist</span><span class="p">,</span> <span class="n">ImproperlyConfigured</span><span class="p">,</span> <span class="n">ValidationError</span><span class="p">,</span>
<span class="p">)</span>
<span class="kn">from</span> <span class="nn">django.utils.functional</span> <span class="k">import</span> <span class="n">lazy</span>
<span class="kn">from</span> <span class="nn">django.utils.html</span> <span class="k">import</span> <span class="n">format_html</span><span class="p">,</span> <span class="n">format_html_join</span>
<span class="kn">from</span> <span class="nn">django.utils.module_loading</span> <span class="k">import</span> <span class="n">import_string</span>
<span class="kn">from</span> <span class="nn">django.utils.translation</span> <span class="k">import</span> <span class="n">gettext</span> <span class="k">as</span> <span class="n">_</span><span class="p">,</span> <span class="n">ngettext</span>


<span class="nd">@functools</span><span class="o">.</span><span class="n">lru_cache</span><span class="p">(</span><span class="n">maxsize</span><span class="o">=</span><span class="kc">None</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">get_default_password_validators</span><span class="p">():</span>
    <span class="k">return</span> <span class="n">get_password_validators</span><span class="p">(</span><span class="n">settings</span><span class="o">.</span><span class="n">AUTH_PASSWORD_VALIDATORS</span><span class="p">)</span>


<div class="viewcode-block" id="get_password_validators"><a class="viewcode-back" href="../../../../topics/auth/passwords.html#django.contrib.auth.password_validation.get_password_validators">[docs]</a><span class="k">def</span> <span class="nf">get_password_validators</span><span class="p">(</span><span class="n">validator_config</span><span class="p">):</span>
    <span class="n">validators</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="k">for</span> <span class="n">validator</span> <span class="ow">in</span> <span class="n">validator_config</span><span class="p">:</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="n">klass</span> <span class="o">=</span> <span class="n">import_string</span><span class="p">(</span><span class="n">validator</span><span class="p">[</span><span class="s1">&#39;NAME&#39;</span><span class="p">])</span>
        <span class="k">except</span> <span class="ne">ImportError</span><span class="p">:</span>
            <span class="n">msg</span> <span class="o">=</span> <span class="s2">&quot;The module in NAME could not be imported: </span><span class="si">%s</span><span class="s2">. Check your AUTH_PASSWORD_VALIDATORS setting.&quot;</span>
            <span class="k">raise</span> <span class="n">ImproperlyConfigured</span><span class="p">(</span><span class="n">msg</span> <span class="o">%</span> <span class="n">validator</span><span class="p">[</span><span class="s1">&#39;NAME&#39;</span><span class="p">])</span>
        <span class="n">validators</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">klass</span><span class="p">(</span><span class="o">**</span><span class="n">validator</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;OPTIONS&#39;</span><span class="p">,</span> <span class="p">{})))</span>

    <span class="k">return</span> <span class="n">validators</span></div>


<div class="viewcode-block" id="validate_password"><a class="viewcode-back" href="../../../../topics/auth/passwords.html#django.contrib.auth.password_validation.validate_password">[docs]</a><span class="k">def</span> <span class="nf">validate_password</span><span class="p">(</span><span class="n">password</span><span class="p">,</span> <span class="n">user</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">password_validators</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Validate whether the password meets all validator requirements.</span>

<span class="sd">    If the password is valid, return ``None``.</span>
<span class="sd">    If the password is invalid, raise ValidationError with all error messages.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">errors</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="k">if</span> <span class="n">password_validators</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
        <span class="n">password_validators</span> <span class="o">=</span> <span class="n">get_default_password_validators</span><span class="p">()</span>
    <span class="k">for</span> <span class="n">validator</span> <span class="ow">in</span> <span class="n">password_validators</span><span class="p">:</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="n">validator</span><span class="o">.</span><span class="n">validate</span><span class="p">(</span><span class="n">password</span><span class="p">,</span> <span class="n">user</span><span class="p">)</span>
        <span class="k">except</span> <span class="n">ValidationError</span> <span class="k">as</span> <span class="n">error</span><span class="p">:</span>
            <span class="n">errors</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">error</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">errors</span><span class="p">:</span>
        <span class="k">raise</span> <span class="n">ValidationError</span><span class="p">(</span><span class="n">errors</span><span class="p">)</span></div>


<div class="viewcode-block" id="password_changed"><a class="viewcode-back" href="../../../../topics/auth/passwords.html#django.contrib.auth.password_validation.password_changed">[docs]</a><span class="k">def</span> <span class="nf">password_changed</span><span class="p">(</span><span class="n">password</span><span class="p">,</span> <span class="n">user</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">password_validators</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Inform all validators that have implemented a password_changed() method</span>
<span class="sd">    that the password has been changed.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">if</span> <span class="n">password_validators</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
        <span class="n">password_validators</span> <span class="o">=</span> <span class="n">get_default_password_validators</span><span class="p">()</span>
    <span class="k">for</span> <span class="n">validator</span> <span class="ow">in</span> <span class="n">password_validators</span><span class="p">:</span>
        <span class="n">password_changed</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">validator</span><span class="p">,</span> <span class="s1">&#39;password_changed&#39;</span><span class="p">,</span> <span class="k">lambda</span> <span class="o">*</span><span class="n">a</span><span class="p">:</span> <span class="kc">None</span><span class="p">)</span>
        <span class="n">password_changed</span><span class="p">(</span><span class="n">password</span><span class="p">,</span> <span class="n">user</span><span class="p">)</span></div>


<div class="viewcode-block" id="password_validators_help_texts"><a class="viewcode-back" href="../../../../topics/auth/passwords.html#django.contrib.auth.password_validation.password_validators_help_texts">[docs]</a><span class="k">def</span> <span class="nf">password_validators_help_texts</span><span class="p">(</span><span class="n">password_validators</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Return a list of all help texts of all configured validators.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">help_texts</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="k">if</span> <span class="n">password_validators</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
        <span class="n">password_validators</span> <span class="o">=</span> <span class="n">get_default_password_validators</span><span class="p">()</span>
    <span class="k">for</span> <span class="n">validator</span> <span class="ow">in</span> <span class="n">password_validators</span><span class="p">:</span>
        <span class="n">help_texts</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">validator</span><span class="o">.</span><span class="n">get_help_text</span><span class="p">())</span>
    <span class="k">return</span> <span class="n">help_texts</span></div>


<span class="k">def</span> <span class="nf">_password_validators_help_text_html</span><span class="p">(</span><span class="n">password_validators</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Return an HTML string with all help texts of all configured validators</span>
<span class="sd">    in an &lt;ul&gt;.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">help_texts</span> <span class="o">=</span> <span class="n">password_validators_help_texts</span><span class="p">(</span><span class="n">password_validators</span><span class="p">)</span>
    <span class="n">help_items</span> <span class="o">=</span> <span class="n">format_html_join</span><span class="p">(</span><span class="s1">&#39;&#39;</span><span class="p">,</span> <span class="s1">&#39;&lt;li&gt;</span><span class="si">{}</span><span class="s1">&lt;/li&gt;&#39;</span><span class="p">,</span> <span class="p">((</span><span class="n">help_text</span><span class="p">,)</span> <span class="k">for</span> <span class="n">help_text</span> <span class="ow">in</span> <span class="n">help_texts</span><span class="p">))</span>
    <span class="k">return</span> <span class="n">format_html</span><span class="p">(</span><span class="s1">&#39;&lt;ul&gt;</span><span class="si">{}</span><span class="s1">&lt;/ul&gt;&#39;</span><span class="p">,</span> <span class="n">help_items</span><span class="p">)</span> <span class="k">if</span> <span class="n">help_items</span> <span class="k">else</span> <span class="s1">&#39;&#39;</span>


<span class="n">password_validators_help_text_html</span> <span class="o">=</span> <span class="n">lazy</span><span class="p">(</span><span class="n">_password_validators_help_text_html</span><span class="p">,</span> <span class="nb">str</span><span class="p">)</span>


<div class="viewcode-block" id="MinimumLengthValidator"><a class="viewcode-back" href="../../../../topics/auth/passwords.html#django.contrib.auth.password_validation.MinimumLengthValidator">[docs]</a><span class="k">class</span> <span class="nc">MinimumLengthValidator</span><span class="p">:</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Validate whether the password is of a minimum length.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">min_length</span><span class="o">=</span><span class="mi">8</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">min_length</span> <span class="o">=</span> <span class="n">min_length</span>

    <span class="k">def</span> <span class="nf">validate</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">password</span><span class="p">,</span> <span class="n">user</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">password</span><span class="p">)</span> <span class="o">&lt;</span> <span class="bp">self</span><span class="o">.</span><span class="n">min_length</span><span class="p">:</span>
            <span class="k">raise</span> <span class="n">ValidationError</span><span class="p">(</span>
                <span class="n">ngettext</span><span class="p">(</span>
                    <span class="s2">&quot;This password is too short. It must contain at least </span><span class="si">%(min_length)d</span><span class="s2"> character.&quot;</span><span class="p">,</span>
                    <span class="s2">&quot;This password is too short. It must contain at least </span><span class="si">%(min_length)d</span><span class="s2"> characters.&quot;</span><span class="p">,</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">min_length</span>
                <span class="p">),</span>
                <span class="n">code</span><span class="o">=</span><span class="s1">&#39;password_too_short&#39;</span><span class="p">,</span>
                <span class="n">params</span><span class="o">=</span><span class="p">{</span><span class="s1">&#39;min_length&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">min_length</span><span class="p">},</span>
            <span class="p">)</span>

    <span class="k">def</span> <span class="nf">get_help_text</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">ngettext</span><span class="p">(</span>
            <span class="s2">&quot;Your password must contain at least </span><span class="si">%(min_length)d</span><span class="s2"> character.&quot;</span><span class="p">,</span>
            <span class="s2">&quot;Your password must contain at least </span><span class="si">%(min_length)d</span><span class="s2"> characters.&quot;</span><span class="p">,</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">min_length</span>
        <span class="p">)</span> <span class="o">%</span> <span class="p">{</span><span class="s1">&#39;min_length&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">min_length</span><span class="p">}</span></div>


<div class="viewcode-block" id="UserAttributeSimilarityValidator"><a class="viewcode-back" href="../../../../topics/auth/passwords.html#django.contrib.auth.password_validation.UserAttributeSimilarityValidator">[docs]</a><span class="k">class</span> <span class="nc">UserAttributeSimilarityValidator</span><span class="p">:</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Validate whether the password is sufficiently different from the user&#39;s</span>
<span class="sd">    attributes.</span>

<span class="sd">    If no specific attributes are provided, look at a sensible list of</span>
<span class="sd">    defaults. Attributes that don&#39;t exist are ignored. Comparison is made to</span>
<span class="sd">    not only the full attribute value, but also its components, so that, for</span>
<span class="sd">    example, a password is validated against either part of an email address,</span>
<span class="sd">    as well as the full address.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">DEFAULT_USER_ATTRIBUTES</span> <span class="o">=</span> <span class="p">(</span><span class="s1">&#39;username&#39;</span><span class="p">,</span> <span class="s1">&#39;first_name&#39;</span><span class="p">,</span> <span class="s1">&#39;last_name&#39;</span><span class="p">,</span> <span class="s1">&#39;email&#39;</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">user_attributes</span><span class="o">=</span><span class="n">DEFAULT_USER_ATTRIBUTES</span><span class="p">,</span> <span class="n">max_similarity</span><span class="o">=</span><span class="mf">0.7</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">user_attributes</span> <span class="o">=</span> <span class="n">user_attributes</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">max_similarity</span> <span class="o">=</span> <span class="n">max_similarity</span>

    <span class="k">def</span> <span class="nf">validate</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">password</span><span class="p">,</span> <span class="n">user</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">user</span><span class="p">:</span>
            <span class="k">return</span>

        <span class="k">for</span> <span class="n">attribute_name</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">user_attributes</span><span class="p">:</span>
            <span class="n">value</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">user</span><span class="p">,</span> <span class="n">attribute_name</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="n">value</span> <span class="ow">or</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">value</span><span class="p">,</span> <span class="nb">str</span><span class="p">):</span>
                <span class="k">continue</span>
            <span class="n">value_parts</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="sa">r</span><span class="s1">&#39;\W+&#39;</span><span class="p">,</span> <span class="n">value</span><span class="p">)</span> <span class="o">+</span> <span class="p">[</span><span class="n">value</span><span class="p">]</span>
            <span class="k">for</span> <span class="n">value_part</span> <span class="ow">in</span> <span class="n">value_parts</span><span class="p">:</span>
                <span class="k">if</span> <span class="n">SequenceMatcher</span><span class="p">(</span><span class="n">a</span><span class="o">=</span><span class="n">password</span><span class="o">.</span><span class="n">lower</span><span class="p">(),</span> <span class="n">b</span><span class="o">=</span><span class="n">value_part</span><span class="o">.</span><span class="n">lower</span><span class="p">())</span><span class="o">.</span><span class="n">quick_ratio</span><span class="p">()</span> <span class="o">&gt;=</span> <span class="bp">self</span><span class="o">.</span><span class="n">max_similarity</span><span class="p">:</span>
                    <span class="k">try</span><span class="p">:</span>
                        <span class="n">verbose_name</span> <span class="o">=</span> <span class="nb">str</span><span class="p">(</span><span class="n">user</span><span class="o">.</span><span class="n">_meta</span><span class="o">.</span><span class="n">get_field</span><span class="p">(</span><span class="n">attribute_name</span><span class="p">)</span><span class="o">.</span><span class="n">verbose_name</span><span class="p">)</span>
                    <span class="k">except</span> <span class="n">FieldDoesNotExist</span><span class="p">:</span>
                        <span class="n">verbose_name</span> <span class="o">=</span> <span class="n">attribute_name</span>
                    <span class="k">raise</span> <span class="n">ValidationError</span><span class="p">(</span>
                        <span class="n">_</span><span class="p">(</span><span class="s2">&quot;The password is too similar to the </span><span class="si">%(verbose_name)s</span><span class="s2">.&quot;</span><span class="p">),</span>
                        <span class="n">code</span><span class="o">=</span><span class="s1">&#39;password_too_similar&#39;</span><span class="p">,</span>
                        <span class="n">params</span><span class="o">=</span><span class="p">{</span><span class="s1">&#39;verbose_name&#39;</span><span class="p">:</span> <span class="n">verbose_name</span><span class="p">},</span>
                    <span class="p">)</span>

    <span class="k">def</span> <span class="nf">get_help_text</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">_</span><span class="p">(</span><span class="s2">&quot;Your password can&#39;t be too similar to your other personal information.&quot;</span><span class="p">)</span></div>


<div class="viewcode-block" id="CommonPasswordValidator"><a class="viewcode-back" href="../../../../topics/auth/passwords.html#django.contrib.auth.password_validation.CommonPasswordValidator">[docs]</a><span class="k">class</span> <span class="nc">CommonPasswordValidator</span><span class="p">:</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Validate whether the password is a common password.</span>

<span class="sd">    The password is rejected if it occurs in a provided list of passwords,</span>
<span class="sd">    which may be gzipped. The list Django ships with contains 20000 common</span>
<span class="sd">    passwords (lowercased and deduplicated), created by Royce Williams:</span>
<span class="sd">    https://gist.github.com/roycewilliams/281ce539915a947a23db17137d91aeb7</span>
<span class="sd">    The password list must be lowercased to match the comparison in validate().</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">DEFAULT_PASSWORD_LIST_PATH</span> <span class="o">=</span> <span class="n">Path</span><span class="p">(</span><span class="vm">__file__</span><span class="p">)</span><span class="o">.</span><span class="n">resolve</span><span class="p">()</span><span class="o">.</span><span class="n">parent</span> <span class="o">/</span> <span class="s1">&#39;common-passwords.txt.gz&#39;</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">password_list_path</span><span class="o">=</span><span class="n">DEFAULT_PASSWORD_LIST_PATH</span><span class="p">):</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="k">with</span> <span class="n">gzip</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">password_list_path</span><span class="p">))</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
                <span class="n">common_passwords_lines</span> <span class="o">=</span> <span class="n">f</span><span class="o">.</span><span class="n">read</span><span class="p">()</span><span class="o">.</span><span class="n">decode</span><span class="p">()</span><span class="o">.</span><span class="n">splitlines</span><span class="p">()</span>
        <span class="k">except</span> <span class="ne">IOError</span><span class="p">:</span>
            <span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">password_list_path</span><span class="p">))</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
                <span class="n">common_passwords_lines</span> <span class="o">=</span> <span class="n">f</span><span class="o">.</span><span class="n">readlines</span><span class="p">()</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">passwords</span> <span class="o">=</span> <span class="p">{</span><span class="n">p</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span> <span class="k">for</span> <span class="n">p</span> <span class="ow">in</span> <span class="n">common_passwords_lines</span><span class="p">}</span>

    <span class="k">def</span> <span class="nf">validate</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">password</span><span class="p">,</span> <span class="n">user</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">password</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">passwords</span><span class="p">:</span>
            <span class="k">raise</span> <span class="n">ValidationError</span><span class="p">(</span>
                <span class="n">_</span><span class="p">(</span><span class="s2">&quot;This password is too common.&quot;</span><span class="p">),</span>
                <span class="n">code</span><span class="o">=</span><span class="s1">&#39;password_too_common&#39;</span><span class="p">,</span>
            <span class="p">)</span>

    <span class="k">def</span> <span class="nf">get_help_text</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">_</span><span class="p">(</span><span class="s2">&quot;Your password can&#39;t be a commonly used password.&quot;</span><span class="p">)</span></div>


<div class="viewcode-block" id="NumericPasswordValidator"><a class="viewcode-back" href="../../../../topics/auth/passwords.html#django.contrib.auth.password_validation.NumericPasswordValidator">[docs]</a><span class="k">class</span> <span class="nc">NumericPasswordValidator</span><span class="p">:</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Validate whether the password is alphanumeric.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">def</span> <span class="nf">validate</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">password</span><span class="p">,</span> <span class="n">user</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">password</span><span class="o">.</span><span class="n">isdigit</span><span class="p">():</span>
            <span class="k">raise</span> <span class="n">ValidationError</span><span class="p">(</span>
                <span class="n">_</span><span class="p">(</span><span class="s2">&quot;This password is entirely numeric.&quot;</span><span class="p">),</span>
                <span class="n">code</span><span class="o">=</span><span class="s1">&#39;password_entirely_numeric&#39;</span><span class="p">,</span>
            <span class="p">)</span>

    <span class="k">def</span> <span class="nf">get_help_text</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">_</span><span class="p">(</span><span class="s2">&quot;Your password can&#39;t be entirely numeric.&quot;</span><span class="p">)</span></div>
</pre></div>

          </div>
        </div>
      </div>
      
        
          <div class="yui-b" id="sidebar">
            
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
<div id="searchbox" style="display: none" role="search">
  <h3>Quick search</h3>
    <div class="searchformwrapper">
    <form class="search" action="../../../../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    </div>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
              <h3>Last update:</h3>
              <p class="topless">Mar 04, 2020</p>
          </div>
        
      
    </div>

    <div id="ft">
      <div class="nav">
    <a href="../../../index.html" title="Module code" accesskey="U">up</a></div>
    </div>
  </div>

      <div class="clearer"></div>
    </div>
  </body>
</html>